<h1>
  <my-global-icon iconName="cloud-download" aria-hidden="true"></my-global-icon>
  <ng-container i18n>My imports</ng-container>
</h1>

<p-table
  [value]="videoImports" [lazy]="true" [paginator]="totalRecords > 0" [totalRecords]="totalRecords" [rows]="rowsPerPage" [rowsPerPageOptions]="rowsPerPageOptions"
  [sortField]="sort.field" [sortOrder]="sort.order" (onLazyLoad)="loadLazy($event)" dataKey="id"
  [showCurrentPageReport]="true" i18n-currentPageReportTemplate
  currentPageReportTemplate="Showing {{'{first}'}} to {{'{last}'}} of {{'{totalRecords}'}} imports"
  (onPage)="onPage($event)" [expandedRowKeys]="expandedRows"
>
  <ng-template pTemplate="header">
    <tr>
      <th style="width: 40px;"></th>
      <th style="width: 70px">Action</th>
      <th style="width: 45%" i18n>Target</th>
      <th style="width: 55%" i18n>Video</th>
      <th style="width: 150px" i18n>State</th>
      <th style="width: 150px" i18n pSortableColumn="createdAt">Created <p-sortIcon field="createdAt"></p-sortIcon></th>
    </tr>
  </ng-template>

  <ng-template pTemplate="body" let-expanded="expanded" let-videoImport>
    <tr>
      <td class="expand-cell">
        <span *ngIf="videoImport.error" class="expander" [pRowToggler]="videoImport" i18n-ngbTooltip ngbTooltip="See the error">
          <i [ngClass]="expanded ? 'glyphicon glyphicon-menu-down' : 'glyphicon glyphicon-menu-right'"></i>
        </span>
      </td>

      <td class="action-cell">
        <my-edit-button *ngIf="isVideoImportSuccess(videoImport) && videoImport.video"
          [routerLink]="getEditVideoUrl(videoImport.video)"></my-edit-button>
      </td>

      <td>
        <a *ngIf="videoImport.targetUrl; else torrent" [href]="videoImport.targetUrl" target="_blank" rel="noopener noreferrer">{{ videoImport.targetUrl }}</a>
        <ng-template #torrent>
          <span [title]="videoImport.torrentName || videoImport.magnetUri">{{ videoImport.torrentName || videoImport.magnetUri }}</span>
        </ng-template>
      </td>

      <td>
        <ng-container *ngIf="isVideoImportPending(videoImport)">{{ videoImport.video?.name }}</ng-container>
        <ng-container *ngIf="isVideoImportSuccess(videoImport) && videoImport.video">
          <a [href]="getVideoUrl(videoImport.video)" target="_blank" rel="noopener noreferrer">{{ videoImport.video?.name }}</a>
        </ng-container>
        <ng-container *ngIf="isVideoImportSuccess(videoImport) && !videoImport.video" i18n>This video was deleted</ng-container>
        <ng-container *ngIf="isVideoImportFailed(videoImport)"></ng-container>
      </td>

      <td>
        <span class="badge" [ngClass]="getVideoImportStateClass(videoImport.state)">
          {{ videoImport.state.label }}
        </span>
      </td>

      <td>{{ videoImport.createdAt | date: 'short' }}</td>
    </tr>
  </ng-template>

  <ng-template pTemplate="rowexpansion" let-videoImport>
    <tr class="video-import-error" *ngIf="videoImport.error">
      <td colspan="6">
        <pre>{{ videoImport.error }}</pre>
      </td>
    </tr>
  </ng-template>
</p-table>
